package interfacesBeans;

/**
 *
 * @author Rodrigo
 */

import excecoes.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;

public class Perfil extends javax.swing.JPanel {

    private String textLabelErro;

    /** Creates new form Perfil */
    public Perfil() {
        initComponents();
        textLabelErro = "";
        setTexts();
    }

    public void setTexts(){
        if (Body.user.getNome() == null) {
            textFieldNome.setText("Digite o seu nome");
        } else {
            textFieldNome.setText(Body.user.getNome());
        }

        if (Body.user.getEmail() == null) {
            textFieldEmail.setText("Digite o seu e-mail");
        } else {
            textFieldEmail.setText(Body.user.getEmail().toString());
        }

        if (Body.user.getDescricao() == null) {
            textAreaDescricao.setText("Informe uma descrição.");
        } else {
            textAreaDescricao.setText(Body.user.getDescricao());
        }

        labelErro.setText(textLabelErro);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Nome = new javax.swing.JLabel();
        labelEmail = new javax.swing.JLabel();
        labelDescricao = new javax.swing.JLabel();
        textFieldNome = new javax.swing.JTextField();
        textFieldEmail = new javax.swing.JTextField();
        scrollPaneDescricao = new javax.swing.JScrollPane();
        textAreaDescricao = new javax.swing.JTextArea();
        buttonOk = new javax.swing.JButton();
        buttonVoltar = new javax.swing.JButton();
        labelPerfil = new javax.swing.JLabel();
        labelFoto = new javax.swing.JLabel();
        labelErro = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        Nome.setFont(new java.awt.Font("Tahoma", 0, 14));
        Nome.setText("Nome");

        labelEmail.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelEmail.setText("E-mail");

        labelDescricao.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelDescricao.setText("Descrição");

        textFieldNome.setFont(new java.awt.Font("Tahoma", 0, 14));
        textFieldNome.setText("Digite o seu nome");
        textFieldNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldNomeActionPerformed(evt);
            }
        });

        textFieldEmail.setFont(new java.awt.Font("Tahoma", 0, 14));
        textFieldEmail.setText("Digite o seu e-mail");
        textFieldEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldEmailActionPerformed(evt);
            }
        });

        textAreaDescricao.setColumns(20);
        textAreaDescricao.setFont(new java.awt.Font("Tahoma", 0, 14));
        textAreaDescricao.setRows(5);
        textAreaDescricao.setText("Informe uma descrição");
        scrollPaneDescricao.setViewportView(textAreaDescricao);

        buttonOk.setText("Ok");
        buttonOk.setPreferredSize(new java.awt.Dimension(60, 23));
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        buttonVoltar.setText("Voltar");
        buttonVoltar.setPreferredSize(new java.awt.Dimension(60, 23));
        buttonVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVoltarActionPerformed(evt);
            }
        });

        labelPerfil.setFont(new java.awt.Font("Tahoma", 1, 28));
        labelPerfil.setText("Perfil do Usuário");

        labelFoto.setText("       Uma foto do usuário");
        if(Body.user.getImagem()!= null) {
            labelFoto.setIcon(Body.user.getImagem());
        }
        labelFoto.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        labelFoto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelFotoMouseClicked(evt);
            }
        });

        labelErro.setFont(new java.awt.Font("Tahoma", 1, 14));
        labelErro.setForeground(new java.awt.Color(255, 0, 0));
        labelErro.setText("Mensagens de erro");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(192, 192, 192)
                        .addComponent(buttonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(labelPerfil))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(55, 55, 55)
                                .addComponent(Nome)
                                .addGap(18, 18, 18)
                                .addComponent(textFieldNome, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(labelEmail)
                                    .addComponent(labelDescricao))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(scrollPaneDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textFieldEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))))
                        .addGap(99, 99, 99)))
                .addGap(22, 22, 22)
                .addComponent(labelFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(96, 96, 96))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(labelErro, javax.swing.GroupLayout.DEFAULT_SIZE, 703, Short.MAX_VALUE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {scrollPaneDescricao, textFieldEmail, textFieldNome});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonOk, buttonVoltar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(labelPerfil)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Nome)
                            .addComponent(textFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelEmail)
                            .addComponent(textFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(scrollPaneDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(buttonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(buttonVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(labelDescricao))
                        .addGap(49, 49, 49))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addComponent(labelFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(135, 135, 135)))
                .addGap(2, 2, 2)
                .addComponent(labelErro, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(78, 78, 78))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buttonOk, buttonVoltar});

    }// </editor-fold>//GEN-END:initComponents

    private void textFieldNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textFieldNomeActionPerformed

    private void textFieldEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldEmailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textFieldEmailActionPerformed

    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed

        textLabelErro = "";

        try {
            Body.user.setNome(textFieldNome.getText());
        } catch (NomeInvalido n) {
            textLabelErro = n.getMessage();
        }

        try {
            Body.user.setEmail(textFieldEmail.getText());
        } catch (FormaDeContatoInvalida e) {
            textLabelErro = e.getMessage();
        }

        Body.user.setDescricao(textAreaDescricao.getText());
        setTexts();
    }//GEN-LAST:event_buttonOkActionPerformed

    private void buttonVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVoltarActionPerformed
        textLabelErro = "";
        setTexts();
        java.awt.CardLayout layout = (java.awt.CardLayout) Body.panelBody.getLayout();
        layout.show(Body.panelBody, "TelaInicial");
    }//GEN-LAST:event_buttonVoltarActionPerformed

private void labelFotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelFotoMouseClicked
    JFileChooser fileChooser = new JFileChooser();
    int result = fileChooser.showOpenDialog(null);
    if(result == JFileChooser.APPROVE_OPTION) {
        URL fotoURL = null;
        
        try {
            fotoURL = fileChooser.getSelectedFile().toURI().toURL();
        } catch( MalformedURLException mfe ) {
            System.err.printf(" Arquivo invalido! ");
        }
        
        if( fotoURL != null ) {
            ImageIcon imagem = new ImageIcon(fotoURL);
            labelFoto.setIcon(imagem);
            Body.user.setImagem(imagem);
        }
        
    }
}//GEN-LAST:event_labelFotoMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Nome;
    private javax.swing.JButton buttonOk;
    private javax.swing.JButton buttonVoltar;
    private javax.swing.JLabel labelDescricao;
    private javax.swing.JLabel labelEmail;
    private javax.swing.JLabel labelErro;
    private javax.swing.JLabel labelFoto;
    private javax.swing.JLabel labelPerfil;
    private javax.swing.JScrollPane scrollPaneDescricao;
    private javax.swing.JTextArea textAreaDescricao;
    private javax.swing.JTextField textFieldEmail;
    private javax.swing.JTextField textFieldNome;
    // End of variables declaration//GEN-END:variables
}
